package com.yanqu.road.server.timer.stronghold;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.logic.cross.stronghold.CrossActivityUnionRankModel;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdActivityMgr;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdRankMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CrossActivityUnionConditionRankSyncTask extends GeneralTask {
    private boolean isRun = false;

    private long lastCheckTime;

    private final int ALL_SYNC_TIME = 30000;      //30s全同步

    @Override
    public void doRun() {
        if(!isRun) {
            isRun = true;
            long nowTime = System.currentTimeMillis();
            List<CrossActivityUnionRankModel> syncList = new ArrayList<>();
            Map<Integer, ActivityInfo> activityInfoMap = CrossStrongholdActivityMgr.getOpenActivityInfoMap();
            if (nowTime - lastCheckTime > ALL_SYNC_TIME) {
                //这边三十秒不要改啊 只能短  不能长
                for(ActivityInfo activityInfo : activityInfoMap.values()){
                    List<CrossActivityUnionRankModel> modelList = CrossStrongholdRankMgr.getCrossActivityUnionRankModelList(activityInfo.getActivityId());
                    syncList.addAll(modelList);
                }
                lastCheckTime = nowTime;
            } else {
                for(ActivityInfo activityInfo : activityInfoMap.values()){
                    List<CrossActivityUnionRankModel> modelList = CrossStrongholdRankMgr.getCrossActivityUnionRankModelList(activityInfo.getActivityId());
                    for (CrossActivityUnionRankModel rankListModel : modelList) {
                        if(null != rankListModel && rankListModel.isNeedSync()){
                            syncList.add(rankListModel);
                        }
                    }
                }
            }
            if (syncList.size() > 0) {
                for (CrossActivityUnionRankModel rankListModel : syncList) {
                    CrossActivityProto.CrossActivityUnionRankListSyncMsg.Builder syncMsg = CrossActivityProto.CrossActivityUnionRankListSyncMsg.newBuilder();
                    syncMsg.setActivityId(rankListModel.getActivityId());
                    syncMsg.setRankList(rankListModel.getRankListMsg());
                    syncMsg.setConditionType(rankListModel.getConditionType());
                    syncMsg.setGroupId(rankListModel.getGroupId());
                    rankListModel.setNeedSync(false);
                    syncToGameServer(syncMsg, activityInfoMap.get(rankListModel.getActivityId()));
                }
            }
            isRun = false;
        }
    }

    private void syncToGameServer(CrossActivityProto.CrossActivityUnionRankListSyncMsg.Builder rankMsg, ActivityInfo activityInfo) {
        if(null == activityInfo || null == rankMsg){
            return;
        }
        for (long serverId : activityInfo.getServerIdList()) {
            MessageHelper.sendPacket(serverId, 0, YanQuMessageUtils.buildMessage(Protocol.S_CROSS_ACTIVITY_UNION_TYPE_RANK_SYNC, rankMsg));
        }
    }
}
